package com.itheima.controllor;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.POIUtils;
import com.itheima.utils.QiniuUtils;
import com.sun.jersey.core.util.StringIgnoreCaseKeyComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;
import service.OrderSettingService;
import service.SetmealService;

import java.io.Serializable;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingControllor {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 预约设置服务
     */
//    Excel文件上传，并解析文件内容保存到数据库
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile){
        try {
            //获取文件的内容,转成 OrderSetting 对象
            List<String[]> oriFile = POIUtils.readExcel(multipartFile);
            ArrayList<OrderSetting> orderSettingList  = new ArrayList();
            OrderSetting singleOrderSetting=null;

            for (String[] rowString : oriFile) {
                 singleOrderSetting = new OrderSetting(new Date(rowString[0]), Integer.parseInt(rowString[1]));
                orderSettingList.add(singleOrderSetting);
            }

            orderSettingService.addOrderSetting(orderSettingList);

            return new Result(true, MessageConstant.UPLOAD_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPLOAD_FAIL);

        }
    }




    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String data){
        try {

            List<Map<String,Object>> list=orderSettingService.getOrderSettingByMonth(data);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL,null);

        }
    }


    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {

            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);

        }
    }


}

